from flask import Flask,jsonify,request,Blueprint,session
from common.res import (getRes,getErrorRes,getSuccessRes,getDataRes)
from common.sqlInjectionSecurity import sqlEscape,sqlEscapeField
from DAL.rightDAL import rolesDAL
from controller.permissionAttr import islog

right_roles = Blueprint('right_roles',__name__)

@right_roles.route("/roles/getlist",methods=['POST'])
@islog
def roles_getlist():
    param = {
        'role_name': request.form.get('role_name') and sqlEscape(request.form.get('role_name')) or "",
        'page': request.form.get('page') and sqlEscape(request.form.get('page')) or 1,
        'limit': request.form.get('limit') and sqlEscape(request.form.get('limit')) or 20,
        'filed': request.form.get('filed') and sqlEscape(request.form.get('filed')) or 'id',
        'sort': request.form.get('sort') and sqlEscape(request.form.get('sort')) or 'ascending'
          }
    dal = rolesDAL()
    data,count = dal.getList(**param)
    if data:
        res = getSuccessRes(data=data,count=count)
    else:
        res = getSuccessRes(data=[], count=count)
    return jsonify(res)
@right_roles.route("/roles/getlist_all",methods=['POST'])
@islog
def getlist_all():
    dal = rolesDAL()
    data = dal.getListAll()
    if data:
        res = getSuccessRes(data=data)
    else:
        res = getSuccessRes(data=[])
    return jsonify(res)


@right_roles.route("/roles/delete_by_id",methods=['POST'])
@islog
def delete_by_id():
    param = {
        'id': request.form.get('id') or None
    }
    if not param['id']:
        res = getErrorRes("你输入的参数有错！")
        return jsonify(res)
    dal = rolesDAL()
    data = dal.deleteById(**param)
    res = getSuccessRes(data=data)
    return jsonify(res)

@right_roles.route("/roles/update",methods=['POST'])
@islog
def update():
    param = {
        'id': request.form.get('id') or None,
        'role_name': request.form.get('role_name') and sqlEscape(request.form.get('role_name')) or None,
    }
    if not param['id']:
        res = getErrorRes("参数id必须填写！")
        return jsonify(res)
    if not param['role_name']:
        res = getErrorRes("角色名称必须填写！")
        return jsonify(res)

    dal = rolesDAL()
    data = dal.update(**param)
    res = getSuccessRes(data=data)
    return jsonify(res)

@right_roles.route("/roles/notExistAdd",methods=['POST'])
@islog
def add():
    param = {
        'role_name': request.form.get('role_name') and sqlEscape(request.form.get('role_name')) or None,
    }
    if not param['role_name']:
        res = getErrorRes("角色名称必须填写！")
        return jsonify(res)

    dal = rolesDAL()
    data = dal.notExistAdd(**param)
    res = getSuccessRes(data=data)
    return jsonify(res)

@right_roles.route("/roles/getModuleIdByRole",methods=['POST'])
@islog
def getModuleIdByRole():
    param = {
        'role_id': request.form.get('role_id') and sqlEscape(request.form.get('role_id')) or None,
    }
    if not param['role_id']:
        res = getErrorRes("参数有误！")
        return jsonify(res)

    dal = rolesDAL()
    data = dal.getModuleIdByRole(role_id=param['role_id'])
    res = getSuccessRes(data=data)
    return jsonify(res)
@right_roles.route("/roles/updateRoletModule",methods=['POST'])
@islog
def updateRoletModule():
    param = {
        'role_id': request.form.get('role_id') and sqlEscape(request.form.get('role_id')) or None,
        'module_ids': request.form.get('module_ids') and sqlEscape(request.form.get('module_ids')) or "",
    }
    if not param['role_id']:
        res = getErrorRes("参数有误！")
        return jsonify(res)
    if param['module_ids']:
        param['module_ids'] = param['module_ids'].split(',')
    else:
        param['module_ids'] = []
    dal = rolesDAL()
    data = dal.updateRoletModule(**param)
    res = getSuccessRes(data=data)
    return jsonify(res)



